package com.ticket.sass.admin.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ticket.sass.admin.dto.input.order.TakeTicketOrderInput;
import com.ticket.sass.admin.mapper.TakeTicketOrderMapper;
import com.ticket.sass.admin.service.BaseService;
import com.ticket.sass.common.entity.tenant.TakeTicketOrderEntity;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ywb
 * @since 2024-05-07
 */
@Service
public class TakeTicketOrderServiceImpl extends ServiceImpl<TakeTicketOrderMapper, TakeTicketOrderEntity> implements BaseService<TakeTicketOrderEntity> {
    public IPage<TakeTicketOrderEntity> list(TakeTicketOrderInput input){
        Page<TakeTicketOrderEntity> page = new Page<>(input.getPageNum(), input.getPageSize());
        QueryWrapper<TakeTicketOrderEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        
        queryWrapper.gt(input.getStartTime() != null, "start_time", input.getStartTime());
        queryWrapper.lt(input.getEndTime() != null, "start_time", input.getEndTime());
        
        queryWrapper.eq(StrUtil.isNotEmpty(input.getChannelCode()), "channel_code", input.getChannelCode());
        queryWrapper.like(StrUtil.isNotEmpty(input.getCellphone()), "u.cellphone", input.getCellphone());
        queryWrapper.isNull("ticket_order.deleted_at");
        queryWrapper.isNull("u.deleted_at");
        
        return this.baseMapper.listQuery(page, queryWrapper);
    }
    
    public TakeTicketOrderEntity count(TakeTicketOrderInput input){
        QueryWrapper<TakeTicketOrderEntity> queryWrapper = new QueryWrapper<>();
        
        queryWrapper.gt(input.getStartTime() != null, "start_time", input.getStartTime());
        queryWrapper.lt(input.getEndTime() != null, "start_time", input.getEndTime());
        
        queryWrapper.eq(StrUtil.isNotEmpty(input.getChannelCode()), "channel_code", input.getChannelCode());
        queryWrapper.like(StrUtil.isNotEmpty(input.getCellphone()), "u.cellphone", input.getCellphone());
        queryWrapper.isNull("ticket_order.deleted_at");
        queryWrapper.isNull("u.deleted_at");
        
        return this.baseMapper.count(queryWrapper);
    }
}
